﻿using FlashGameSite.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FlashGameSite
{
    public partial class FormLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            using (FlashGamesDataContext dataContext = new Manager.FlashGamesDataContext())
            {
                List<UserInfo> userInfos = dataContext.UserInfos.Where(obj => (obj.Username == txtUsername.Text.Trim() && obj.Password==txtPassword.Text)).ToList();
                if (userInfos.Count > 0)
                {
                    Response.Cookies.Add(new HttpCookie("fguid", string.Format("{0}/{1}", userInfos[0].UserID, userInfos[0].FullName)));
                    Response.Cookies["fguid"].Expires = DateTime.Now.AddHours(5);
                    if (Request.QueryString.AllKeys.Contains("prev"))
                    {
                        Response.Redirect(Request.QueryString["prev"]);
                    }
                    else
                    {
                        Response.Redirect("MyWall.aspx");
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Invalid username and password!');", true);
                }
            }
        }

    
    }
}